package com.web.action;

import java.io.UnsupportedEncodingException;
import java.util.List;

import org.common.util.TranslateHelper;
import org.wface.web.core.ActionSupport;

import com.manager.service.news.CategoryService;
import com.web.model.Category;
import com.web.model.News;
import com.web.service.NewsService;
import com.web.tag.bean.PageModel;

public class MainAction extends ActionSupport {

	private String keyword;
	private String page;

	public String main() {
		CategoryService cateService = new CategoryService();
		NewsService newsService=new NewsService();
		List<Category> categorys = cateService.findShow();
		getRequest().getSession().getServletContext().setAttribute("categorys", categorys);
		
		PageModel model = new PageModel();
		model.setCountPage(4);
		model.setPageNum(0);
		int title1=41;  //公司新闻
		int title2=161; //成功案例
		int title3=42;  //市场活动
		int title4=63;  //媒体报道
		List list1=newsService.listNewsByCat(title1, model);
		List list2=newsService.listNewsByCat(title2, model);
		List list3=newsService.listNewsByCat(title3, model);
		List list4=newsService.listNewsByCat(title4, model);
		getRequest().setAttribute("t1", "m1=1&m2=3&m3=41"); 
		getRequest().setAttribute("t2","m1=61&m2=161"); 
		getRequest().setAttribute("t3","m1=1&m2=3&m3=42");
		getRequest().setAttribute("t4","m1=1&m2=3&m3=63");
		getRequest().setAttribute("t1List",list1); 
		getRequest().setAttribute("t2List",list2);
		getRequest().setAttribute("t3List",list3); 
		getRequest().setAttribute("t4List",list4); 
		
		return "main";
	}
	public String menu() {
		CategoryService cateService = new CategoryService();
		List<Category> categorys = cateService.findShow();
		getRequest().getSession().getServletContext().setAttribute("categorys", categorys);
		return "menu";
	}

	public String search() {
		PageModel model = new PageModel();
		int pageNum = TranslateHelper.parseInteger(page);
		model.setPageNum(pageNum);
		try {
			keyword = new String(keyword.getBytes("ISO8859-1"), "utf-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		NewsService newsService = new NewsService();
		List<News> list = newsService.searchNews(keyword, model);
		getRequest().setAttribute("list", list);
		if (keyword == null || keyword.trim().isEmpty())
			getRequest().setAttribute("keyword", "全部");
		else
			getRequest().setAttribute("keyword", keyword);

		model.setPaths(getBasePath() + "/search.do?keyword=" + keyword + "&");
		getRequest().setAttribute(PageModel.PAGEMODEL, model);
		return "search";
	}

	private String getBasePath() {
		String basePath = getRequest().getScheme() + "://" + getRequest().getServerName() + ":" + getRequest().getServerPort() + getRequest().getContextPath();
		return basePath;
	}

	public void setKeyword(String keyword) {
		this.keyword = keyword;
	}

	public void setPage(String page) {
		this.page = page;
	}

}
